package com.adinnet.admin.schoolInform.controller;


import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.organization.service.OrganizationService;
import com.adinnet.admin.school.service.SchoolService;
import com.adinnet.admin.schoolInform.model.SchoolInform;
import com.adinnet.admin.schoolInform.model.form.SchoolInformForm;
import com.adinnet.admin.schoolInform.model.vo.SchoolInformReq;
import com.adinnet.admin.schoolInform.model.vo.SchoolInformResp;
import com.adinnet.admin.schoolInform.service.SchoolInformService;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.common.biz.enums.RoleCode;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.adinnet.core.exception.BizException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 校区消息（管理端菜单） 前端控制器
 * </p>
 *
 * @author chenjia
 * @since 2020-12-21
 */
@RestController
@RequestMapping("admin/schoolInform")
public class SchoolInformController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private SchoolInformService schoolInformService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private SchoolService schoolService;


    /**
     * 消息管理页面
     * 1，返回用户可查询的机构列表
     * 2，返回用户可查询的校区列表
     * @param model
     * @return
     */
    @GetMapping()
    public ModelAndView schoolInform(Model model){
        SysUserInfo sysUserInfo=commonService.getLoginUser();
        model.addAttribute("organizationsList", organizationService.getListByUserRole(sysUserInfo));
        model.addAttribute("schoolList", schoolService.getListByUserRole(sysUserInfo));
        return new ModelAndView("schoolInform/schoolInform.html");
    }

    /**
     * 消息管理列表
     * 默认页面
     * 1，超级管理员查所有
     * 2，机构查机构下的数据
     * 3，校区查校区的数据
     * @param schoolInformForm
     * @return
     */
    @GetMapping("/list")
    public PageResult<SchoolInformResp> list(SchoolInformForm schoolInformForm) {
        SysUserInfo sysUser= commonService.getLoginUser();
        //默认页面处理和重置搜索的处理
        if(schoolInformForm.getClassId()==null&&schoolInformForm.getOrganizationId()==null&&schoolInformForm.getSchoolId()==null){
            if(sysUser!=null){
                switch (sysUser.getMaxRole()){
                    case "admin":
                        break;
                    case "organization":
                        schoolInformForm.setOrganizationId(sysUser.getOrganizationId());
                        break;
                    case "school":
                        schoolInformForm.setSchoolId(sysUser.getSchoolId());
                        break;
                    case "classs":
                        schoolInformForm.setSysUserId(sysUser.getId());
                        break;
                    default:
                        break;
                }
            }
        }
        if(schoolInformForm.getSchoolId()!=null){
            schoolInformForm.setOrganizationId(null);
        }
        if(RoleCode.SCHOOL.getCode().equals(sysUser.getMaxRole())){
            schoolInformForm.setSchoolSchoolId(sysUser.getSchoolId());
        }
        schoolInformForm.setMaxRoleCode(sysUser.getMaxRole());
        return schoolInformService.queryList(schoolInformForm);
    }

    /**
     * 批量删除消息
     */
    @RequestMapping("deleteIds")
    @ResponseBody
    public JsonResult deleteIds(String informIds, HttpServletRequest request) {
        commonService.saveOperation(request,"消息管理-删除消息：删除的消息id为："+informIds);
        return schoolInformService.deleteIds(informIds);
    }


    /**
     * 新增消息
     **/
    @ResponseBody
    @RequestMapping("add")
    public JsonResult add(SchoolInformReq schoolInformReq, HttpServletRequest request) {
        SchoolInform schoolInform = new SchoolInform();
        BeanUtils.copyProperties(schoolInformReq,schoolInform);

        SysUserInfo sysUserInfo=commonService.getLoginUser();
        if(sysUserInfo==null){
            throw new BizException("登录过期，请重新登录");
        }
        if(RoleCode.ORGANIZATION.getCode().equals(sysUserInfo.getMaxRole())){
            schoolInform.setCreatedOrganizationId(sysUserInfo.getOrganizationId());
        }
        if(RoleCode.SCHOOL.getCode().equals(sysUserInfo.getMaxRole())){
            schoolInform.setCreatedSchoolId(sysUserInfo.getSchoolId());
        }
        schoolInform.setStatus(0);
        if (schoolInformService.save(schoolInform)) {
            commonService.saveOperation(request,"消息管理-新增消息,资讯id为："+schoolInform.getId());
            return JsonResult.ok("添加成功");
        } else {
            return JsonResult.error("添加失败");
        }
    }

    /**
     * 编辑消息
     **/
    @ResponseBody
    @RequestMapping("update")
    public JsonResult update(SchoolInformReq schoolInformReq, HttpServletRequest request) {
        SchoolInform schoolInform = new SchoolInform();
        BeanUtils.copyProperties(schoolInformReq,schoolInform);
        if (schoolInformService.updateById(schoolInform)) {
            commonService.saveOperation(request,"消息管理-编辑消息,消息id为："+schoolInform.getId());
            return JsonResult.ok("修改成功");
        } else {
            return JsonResult.error("修改失败");
        }
    }


    /**
     * 发送
     **/
    @ResponseBody
    @RequestMapping("send")
    public JsonResult send(SchoolInformReq schoolInformReq, HttpServletRequest request) {
        Integer type=3;
        SysUserInfo sysUser= commonService.getLoginUser();
        if(sysUser!=null&&"admin".equals(sysUser.getMaxRole())){
            type=4;
        }
        SchoolInform schoolInform = new SchoolInform();
        BeanUtils.copyProperties(schoolInformReq,schoolInform);
        schoolInform.setStatus(1);
        schoolInformService.sendInform(schoolInformReq,type);
        if (schoolInformService.updateById(schoolInform)) {
            commonService.saveOperation(request,"消息管理-发送消息,消息id为："+schoolInform.getId());
            return JsonResult.ok("发送成功");
        } else {
            return JsonResult.error("发送失败");
        }
    }
}
